package com.xbsg.protocol.xf.entiy;

import lombok.Data;

@Data
public class Protocol {
    //启动符‘@@’（2 字节）
    public String start;
    /*控制单元start*/
    //业务流水
    public String yewuliushui;
    //协议版本号
    public String xieyibanbenhao;
    //时间标签
    public String time_stamp;
    //源地址
    public String source_address;
    //目标地址
    public String target_address;
    //应用数据单元长度
    public String yingyong_data_length;
    // 应用数据单元
    public String control;
    /*控制单元end*/

    // 应用数据单元
    public String yingyong_data ="";
    // 校验和
    public String jiaoyan;
    //结束符‘##’
    public String end;


    public String toString() {
        return "this{" +
                "start='" + this.start + '\'' +
                ", yewuliushui='" + this.yewuliushui + '\'' +
                ", xieyibanbenhao='" + this.xieyibanbenhao + '\'' +
                ", time_stamp='" + this.time_stamp + '\'' +
                ", source_address='" + this.source_address + '\'' +
                ", target_address='" + this.target_address + '\'' +
                ", yingyong_data_length='" + this.yingyong_data_length + '\'' +
                ", control='" + this.control + '\'' +
                ", yingyong_data='" + this.yingyong_data + '\'' +
                ", jiaoyan='" + this.jiaoyan + '\'' +
                ", end='" + this.end + '\'' +
                '}';
    }

    public Protocol(String message){

        String[] temps = message.split(" ");
        //0-1 启动字符
        this.start = hex2tenString(temps[0])+hex2tenString(temps[1]);
        // 2/3 低字节在前
        this.yewuliushui =temps[3]+temps[2];
        //4/5 主版本，用户版本
        this.xieyibanbenhao = temps[4]+temps[5];
        //6/11 低字节在前
        this.time_stamp =  temps[11]+" "+temps[10]+" " + temps[9]+" "+temps[8]+" "+temps[7]+" "+temps[6];

        //12/17 低字节在前
        this.source_address = temps[17]+temps[16]+temps[15]+temps[14]+temps[13]+temps[12];
        //18/23 低字节在前
        this.target_address = temps[23]+temps[22]+temps[21]+temps[20]+temps[19]+temps[18];
        //24/25 data_len 低字节在前
        this.yingyong_data_length = temps[25] + temps[24];
        //26 control
        this.control = temps[26];
        for(int i = 27;i<27+Integer.parseInt(this.yingyong_data_length);i++){
            this.yingyong_data += temps[i] + " ";
        }

        this.jiaoyan = hex2tenString(temps[temps.length-3]);
        this.end = hex2tenString(temps[temps.length-2])  + hex2tenString(temps[temps.length-1]);
        System.out.println(this.toString());
        //27/27+len-1
        //length-1-2-1
        //length-3/length-1
//        for(int i =0;i<temps.length;i++){
//
//        }
    }

    public String hex2tenString(String hexString) {
        // 16进制字符串
        int decimalValue = Integer.parseInt(hexString, 16); // 将16进制字符串转换为10进制整数
        String decimalString = String.valueOf(decimalValue); // 将10进制整数转换为字符串
        return decimalString; // 输出10进制字符串
    }

    public String get_time_stamp(){
        if (this.time_stamp != null){
            return AppDataUtil.parse_time_stamp(this.time_stamp);
        }
        return this.time_stamp;
    }
}
